const crypto = require('crypto')
const dbPool = require('../../conf/db.js')
const template = require('../../conf/template.js')
const {
	generateToken,
	verifyToken,
	generateName
} = require('../common/common.js')

/* 注册 */
function register(req, res, next) {
	// MD5加密密码
	let passwordMD5 = crypto.createHash('md5').update(req.body.password, 'utf8').digest('hex')
	// 随机昵称
	let nameString = generateName()
	//sql语句
	let sql =
		`insert into user(user,password,user_name) values ('${req.body.user}','${passwordMD5}','${nameString}')`
	let sql2 = `select user from user where user=${req.body.user}`

	dbPool.query(sql2, (err, rows) => {
		if (rows.length > 0) {
			res.send(template(199, [], '失败：该用户已存在'))
			console.log('注册:' + req.body.user + '失败：该用户已存在')
			return
		} else {
			dbPool.execute(sql, (err, rows) => {
				if (!err) {
					res.send(template(200, [], '成功'))
					console.log('注册:' + req.body.user + '成功')
				} else {
					res.send(template(199, [], '失败'))
					console.log('注册:' + req.body.user + '失败')
				}
			})
		}
	})
}

/* 登录 */
function login(req, res, next) {
	let sql = `select * from user where user = '${req.body.user}' and password = '${req.body.password}'`
	dbPool.query(sql, (err, rows) => {
		console.log(rows)
		if (rows.length <= 0) {
			res.send(template(199, [], '用户不存在'))
		} else {
			let token = generateToken(rows[0].user_id)
			let sql2 = `update user set token = '${token}' where user = '${req.body.user}'`
			dbPool.execute(sql2, (err, rows) => {
				console.log('token已更新')
			})
			res.send(template(200, {
				token
			}, '登录成功！'))
		}
	})
}

/* 修改密码 */
function resetPassword(req, res, next) {
	//账号
	let user = req.body.user
	//原密码
	let primaryPassword = req.body.primary_password
	//现密码
	let nowPassword = req.body.now_password
	let sql1 = `select * from user where user='${user}'`
	dbPool.query(sql1, (err, rows) => {
		if (rows[0].password == primaryPassword) {
			console.log(`${user}重置-原密码验证通过`)
			// MD5加密密码
			let nowPasswordMD5 = crypto.createHash('md5').update(nowPassword, 'utf8').digest('hex')
			let sql2 = `update user set password = '${nowPasswordMD5}' where user = '${user}'`
			dbPool.execute(sql2, (err, rows) => {
				if (!err) {
					res.send(template(200, {
						isOK: true
					}, '密码修改成功'))
				} else {
					res.send(template(199, {
						isOK: false
					}, '密码修改失败'))
				}
			})
		} else {
			console.log(`${user}重置-原密码不正确`)
			res.send(template(199, {
				isOK: false
			}, '原密码不正确'))
		}

	})
}

/* 修改用户信息 */
function editUserInfo(req, res, next) {
	let sql = ''
	if (req.body.user_name != '' && req.body.user_name != undefined) {
		sql = `update user set user_name='${req.body.user_name}' where user_id = ${req.body.user_id}`
		dbPool.execute(sql, (err, rows) => {
			if (!err) {
				res.send(template(200, {
					isOK: true
				}, '修改成功'))
			} else {
				res.send(template(199, {
					isOK: false
				}, '修改失败'))
			}
		})
		return
	}
	res.send(template(199, {
		isOK: false
	}, '请填写完整信息'))
}

function test(req, res, next) {
	let a = verifyToken(
		'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDI0OTk0MzEsImRhdGEiOjcwLCJpYXQiOjE2NDI0OTU4MzF9.072fvQ9SD0Cq9aRqWrmv22LQvlFLr1mapukt2HJQzYM'
	)
	if (a) {
		console.log(a)
	}
}

module.exports = {
	register,
	login,
	resetPassword,
	editUserInfo,
	test
}
